package com.smartrobot.smartrobot.circle.NewFriend;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import android.view.View;
//import com.readystatesoftware.viewbadger.BadgeView;
import com.smartrobot.smartrobot.Common.CommonFunc;
import com.smartrobot.smartrobot.DB.NewFriendSqlHelper;
import com.smartrobot.smartrobot.activity.New_friend_activity;

import com.smartrobot.smartrobot.adapter.SocialListViewAdapter;
import com.smartrobot.smartrobot.circle.FriendList;

import com.smartrobot.smartrobot.circle.commonData;
import com.smartrobot.smartrobot.logToSdCard.LogToSd;
import com.smartrobot.smartrobot.pagerHolder.SocialCirlePagerHolder;
import com.smartrobot.smartrobot.pojo.NewFriendPojo;
import com.smartrobot.smartrobot.pojo.getFriendListPojo;
import android.os.Message;
import com.smartrobot.smartrobot.util.User;
import org.json.JSONObject;

//import java.util.ArrayList;
import java.util.List;

/**
 * Created by yeluojun on 2015/1/20.
 */
public class NewFriendUtil {
    public static String NEW_TIP = "newFriendTip";
    private NewFriendSqlHelper DBHelper;
    private Context context;

    public NewFriendUtil(Context context, String userId) {
        this.context = context;
        DBHelper = new NewFriendSqlHelper(context, userId);


    }


    public NewFriendUtil(Context context){
        this.context = context;

    }


    //初始化
    public void init(Context context, String userId) {
        this.context = context;
        DBHelper = new NewFriendSqlHelper(context, userId);

    }

    //保存数据
    public int saveData(JSONObject tempJson) {

        //只有这个程序才可以打开这个文件 保存用户信息

        boolean success = false;
        int i = -1;
        getFriendListPojo pojo = changeData(tempJson);
        if (pojo != null) {
            DBHelper.open();
            if (DBHelper.ifExist(pojo.getUserId())) {
                success = DBHelper.updateAllFromNewFriend(pojo, "");
                if(success){
                    i= 0;
                }
            } else {
                success = DBHelper.insertNewFriend(pojo, "") > 0;
                if(success){
                    i=1;
                }
            }
            DBHelper.close();
        }
        return i;
    }


    public    String  getNewFriendTip(){
        String s;
        try{
            if(User.getInstance()==null){
                User.getUserInfo(context);
            }
            SharedPreferences sharedPreferences = context.getSharedPreferences(NEW_TIP, Context.MODE_PRIVATE);


            s = sharedPreferences.getString((User.getInstance().getPhone() + "tip_num"), "");


        }catch (Exception e){
           s =  "";
        }
        return s;
    }


    public  void saveNewFriendTip(String s){
        try{
            if(User.getInstance()==null){
                User.getUserInfo(context);
            }
            SharedPreferences sharedPreferences = context.getSharedPreferences(NEW_TIP, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();//获得编辑器
            editor.putString((User.getInstance().getPhone() + "tip_num"),s);
            editor.commit();
        }catch (Exception e){
            LogToSd.log_init();
            LogToSd.error("保存未读的新的朋友失败");
            LogToSd.error(e.toString());
        }
    }


    public synchronized  void saveAndShowNewFriendTip(String s,int flag){

        saveNewFriendTip(s);
        Message message = new Message();
        message.what = commonData.SHOW_FRIEND_TIP_NUM;
        message.arg1 = flag;


        //显示“new”
        if(SocialListViewAdapter.getFriendListHandler!=null){
            SocialListViewAdapter.getFriendListHandler.sendMessage(message);
        }



    }


    //获取数据
    public List<NewFriendPojo> getData() {
        DBHelper.open();
        List<NewFriendPojo> list = DBHelper.searchFromNewFriend();
        DBHelper.close();
        return list;
    }

    //更新数据
    public void updateStatus(String status, String userId) {
        DBHelper.open();
        DBHelper.updateFromNewFriend(status, userId);
        DBHelper.close();
    }

    //格式化数据
    public getFriendListPojo changeData(JSONObject tempJson) {
        try {
            if (tempJson != null) {

                getFriendListPojo pojo = new getFriendListPojo();
                pojo.setHeadImg(tempJson.getString("u_head"));
                pojo.setName(tempJson.getString("u_nickname"));
                pojo.setSex(tempJson.getString("u_sex"));
                pojo.setSign(tempJson.getString("u_signature"));
                pojo.setUserId(tempJson.getString("u_id"));
                pojo.setUserAccount(tempJson.getString("u_phone"));
                pojo.setReName(tempJson.getString("u_name"));
                pojo.setRole(tempJson.getString("u_role"));
                if (!pojo.getUserId().equals("")) {
                    return pojo;
                }

            }
        } catch (Exception e) {
            LogToSd.log_init();
            LogToSd.error("接收添加好友请求失败");
            LogToSd.error(e.toString());
        }
        return null;
    }


    //在New_friend_activity页面时当场改变
    public void addPojo(JSONObject tempJson,String status) {
        getFriendListPojo pojo = changeData(tempJson);
        NewFriendPojo newFriendPojo = new NewFriendPojo(pojo.getHeadImg(), pojo.getName(), pojo.getSex(), pojo.getUserId(), pojo.getUserAccount(), pojo.getSign(), pojo.getRole(), pojo.getReName(), "", NewFriendSqlHelper.UN_NOK);



        if (New_friend_activity.instance != null) {
            New_friend_activity.instance.changeOnly(newFriendPojo,status);
        } else {
            Log.e("========初始化单例失败======", "");
        }
    }


    //refresh friend list更新朋友列表
    public void refreshFriendList(JSONObject tempJson) {

        getFriendListPojo pojo = changeData(tempJson);
        FriendList friendList = new FriendList();
        friendList.getFriendListBackGroup(SocialListViewAdapter.getFriendListHandler, context);


        //删除新的朋友数据
        DBHelper.open();
        DBHelper.deleteFromNewFriend(pojo.getUserId());
        DBHelper.close();


        //删除新的朋友的相应数据
        if(New_friend_activity.instance!=null){
            New_friend_activity.instance.removeList(pojo.getUserId());
        }

        Notify.fResponseNotify(context,pojo);
    }






}
